//
//  FindViewController.m
//  CBT
//
//  Created by Mr.Zhang on 2019/1/2.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "FindViewController.h"

#import "FindCell.h"

#import "FindCollectionViewCell.h"

#import "FindModel.h"

@interface FindViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation FindViewController

#pragma mark - VC Life

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = kLocalizedString(@"发现", nil);
    
    [self initTableView];
}

-(void)initTableView
{
    if (@available(iOS 11.0, *)){
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
     self.tableView.estimatedRowHeight           = 0;
     self.tableView.estimatedSectionHeaderHeight = 0;
     self.tableView.estimatedSectionFooterHeight = 0;
    
    self.tableView.backgroundColor = UIColorFromRGB(0xf1f1f1);
    
    [self.tableView reloadData];
}

#pragma mark - ChangeLanguageNotification

- (void)changeLanguage
{
     self.navigationItem.title = kLocalizedString(@"发现", nil);
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    @weak(self)
    FindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"findCellID"];
    cell.model = self.dataArray[indexPath.section];
    [cell setFindCellSelectBlock:^(NSString * _Nonnull itemName) {
        [self_weak_ jumpToLocalBrowserWithURL:[self_weak_ getURLWithItemName:itemName]];
    }];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 236;
    }
    return 148;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 11;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"tableView%ld,%ld",(long)indexPath.section,(long)indexPath.row);
}

#pragma mark - Lazy

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        
        for (int i = 0; i < 2; i++) {
            FindModel *model = [[FindModel alloc]init];
            model.sectionIconName = i == 0 ? @"faxian_yingyong" : @"faxian_meiti";
            model.sectionText = kLocalizedString(i == 0 ? @"应用" : @"媒体", nil);
            
            for (int j = 0; j < (i == 0 ? 5 : 2); j++) {
                FindItemModel *itemModel = [[FindItemModel alloc]init];
                itemModel.iconName = [self getIconNameWithIndex:i row:j];
                itemModel.title = [self getCellTitleNameWithIndex:i row:j];
                [model.itemModelArray addObject:itemModel];
            }
            [_dataArray addObject:model];
        }
    }
    return _dataArray;
}

-(NSString *)getIconNameWithIndex:(NSInteger)index row:(int)row
{
    if (index == 0) {
        switch (row) {
            case 0:{return @"chaoex";}break;
            case 1:{return @"huojv";}break;
            case 2:{return @"eth";}break;
            case 3:{return @"btc.com";}break;
            case 4:{return @"okex";}break;
        }
    }
    else if (index == 1) {
        switch (row) {
            case 0:{return @"sbs";}break;
            case 1:{return @"publish";}break;
        }
    }
    return @"";
}

-(NSString *)getCellTitleNameWithIndex:(NSInteger)index row:(int)row
{
    if (index == 0) {
        switch (row) {
            case 0:{return @"CHAOEX";}break;
            case 1:{return @"火炬";}break;
            case 2:{return @"以太坊浏览器";}break;
            case 3:{return @"比特币浏览器";}break;
            case 4:{return @"OKEX";}break;
            
        }
    }
    else if (index == 1){
        switch (row) {
            case 0:{return @"金色财经";}break;
            case 1:{return @"火星财经";}break;
        }
    }
    return @"";
}

-(NSString *)getURLWithItemName:(NSString *)name
{
    if ([name isEqualToString:kLocalizedString(@"火炬", nil)]) {
        return @"https://huoju.global/";
    }
    else if ([name isEqualToString:kLocalizedString(@"比特币浏览器", nil)]) {
        return @"https://www.btc.com";
    }
    else if ([name isEqualToString:kLocalizedString(@"火币", nil)]) {
        return @"https://www.huobi.co";
    }
    else if ([name isEqualToString:kLocalizedString(@"OKEX", nil)]) {
        return @"https://www.okex.me";
    }
    else if ([name isEqualToString:kLocalizedString(@"以太坊浏览器", nil)]) {
        return @"https://etherscan.io";
    }
    else if ([name isEqualToString:kLocalizedString(@"ZB", nil)]) {
        return @"https://www.zb.cn";
    }
    else if ([name isEqualToString:kLocalizedString(@"金色财经", nil)]) {
        return @"https://www.jinse.com";
    }
    else if ([name isEqualToString:kLocalizedString(@"火星财经", nil)]) {
        return @"https://m.huoxing24.com/";
    }
    else if ([name isEqualToString:kLocalizedString(@"Publish", nil)]) {
        return @"https://publishprotocol.io/cn";
    }
    else if ([name isEqualToString:kLocalizedString(@"SBS", nil)]) {
        return @"http://www.sbs.co.kr/";
    }
    else if ([name isEqualToString:@"CHAOEX"]) {
        return @"https://www.chaoex.info/download";
    }
    else{
        return @"";
    }
}

@end
